# 构建阶段
FROM node:20.9.0-alpine AS build-stage

# 使用阿里镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 安装必要工具
RUN apk update && apk add git jq

# 设置工作目录
WORKDIR /app

#私有仓库账号 注意账户中带@用%40代替
ARG GIT_USERNAME=''
ARG GIT_PASSWORD=''

# 从Git拉取代码
RUN git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@gitee.com/gaogao520/good-medical-skills---admin.git .

# 安装pnpm并使用国内镜像源
RUN npm config set registry https://registry.npmmirror.com/ && npm install -g pnpm

# 临时修改package.json移除prepare脚本
RUN jq 'del(.scripts.prepare)' package.json > package.tmp.json && \
    mv package.tmp.json package.json

# 清理npm缓存并安装依赖
RUN npm cache clean --force && \
    HUSKY=0 pnpm install --no-frozen-lockfile

# 可选：查看package.json中的build脚本
RUN echo "Build脚本内容:" && jq '.scripts.build' package.json

# 构建生产版本
RUN pnpm --verbose build || (echo "构建失败，请检查错误信息"; exit 1)

# 生产运行环境
FROM nginx:alpine AS production

# 复制Nginx配置文件
COPY nginx.conf /etc/nginx/conf.d/default.conf

# 复制构建产物
COPY --from=build-stage /app/dist /usr/share/nginx/html

# 暴露HTTP端口
EXPOSE 9603

# 启动Nginx
CMD ["nginx", "-g", "daemon off;"]